package com.ruoyi.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.business.entity.YytMember;
import com.ruoyi.common.oms.dao.YytOmsOrderItemMapper;
import com.ruoyi.common.oms.dao.YytOmsOrderMapper;
import com.ruoyi.common.oms.entity.YytOmsCartItem;
import com.ruoyi.common.oms.entity.YytOmsOrder;
import com.ruoyi.common.oms.entity.YytOmsOrderItem;
import com.ruoyi.common.oms.service.YytOmsCartItemService;
import com.ruoyi.common.oms.service.YytOmsOrderService;
import com.ruoyi.utils.LoginUtils;
import com.ruoyi.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: MrPing
 * @createTime: 2022/3/3 10:21
 * @description:
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {


    @Autowired
    private YytOmsOrderMapper yytOmsOrderMapper;

    @Autowired
    private YytOmsCartItemService yytOmsCartItemService;

    @Autowired
    private YytOmsOrderItemMapper yytOmsOrderItemMapper;

    /**
     * 退款/售后
     * @param userUuid
     * @param startIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/searchOrderAfterSale")
    public ResponseResult searchOrderAfterSale(@RequestParam("userUuid") String userUuid,
                                               @RequestParam("startIndex") Integer startIndex,
                                               @RequestParam("pageSize") Integer pageSize
                                                ){
        IPage<YytOmsOrder> iPage = yytOmsOrderMapper.getOrderReturns(new Page<>(startIndex, pageSize), userUuid);
        List<YytOmsOrder> records = iPage.getRecords();
        List<YytOmsOrder> result = new ArrayList<>();
        for (YytOmsOrder y: records){
            QueryWrapper<YytOmsOrderItem> wrapper = new QueryWrapper<>();
            wrapper.eq("order_id", y.getId());
            y.setItemList(yytOmsOrderItemMapper.selectList(wrapper));
            result.add(y);
        }
        iPage.setRecords(result);
        return ResponseResult.success(iPage);
    }

    /**
     * 清空购物车
     * @param memberId
     * @return
     */
    @GetMapping("/clearShoppingCart")
    public ResponseResult clearCartItem(@RequestParam("memberId") String memberId){
        UpdateWrapper<YytOmsCartItem> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("member_id", memberId).set("del_flag", 2);
        boolean update = yytOmsCartItemService.update(updateWrapper);
        return ResponseResult.success(update);
    }

    @PostMapping("/deleteOneCartItem")
    public ResponseResult deleteOneCartItem(@RequestBody Map cartItems){
        List<String> list = (List<String>) cartItems.get("cartItems");
        for (int i = 0; i < list.size(); i ++){
            System.out.println(list);
            UpdateWrapper<YytOmsCartItem> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", list.get(i)).set("del_flag", 2);
            yytOmsCartItemService.update(updateWrapper);
        }
        return ResponseResult.success();
    }


    /**
     * 查询当前本月已付款的订单
     * @return
     */
    @GetMapping("/getPerformanceList")
    public ResponseResult getPerformanceList(@RequestParam("memberId") String memberId){
        return ResponseResult.success();
    }

}
